package splug.cjk;

import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

import splug.cjk.packet.BBSCarPoolPacket;
import splug.cjk.packet.BBSCommentPacket;

public class CarPoolTab2CommentRead extends Thread{
	
	private OutputStream os;
	private int number;
	
	public CarPoolTab2CommentRead(OutputStream os, int number) {
		this.os = os;
		this.number = number;
	}
	
	public void run(){
		try {
			Class.forName("com.mysql.jdbc.Driver");
			Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/carpool?useUnicode=true&characterEncoding=euckr","root","apmsetup");
			Statement stmt = conn.createStatement();

			String tmp_query1 = "select id, comment from carpooltab2comment where number = " 
				+ number + " order by comment_number";

			stmt.execute("SET CHARACTER SET euckr");
			stmt.execute("set names euckr");

			ResultSet rs1 = stmt.executeQuery(tmp_query1);
			
			ArrayList<Content> content = new ArrayList<Content>();			

			while(rs1.next()){
				String str1 = rs1.getString("comment");
				String str2 = rs1.getString("id");
				Content c = new Content(str1, str2);
				content.add(c);
			}
			
			BBSCommentPacket packet = new BBSCommentPacket(content, 0, "", "");
			ObjectOutputStream output = new ObjectOutputStream(os);
			output.writeObject(packet);
			
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
}
